package com.leo;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @ClassName ChoiceBoxDemo
 * @Description
 * @Author wangss
 * @date 2020.05.24 21:32
 * @Version 1.0
 */
public class ChoiceBox2Demo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(anchorPane);

        ObservableList<String> listStr = FXCollections.observableArrayList();
        listStr.addAll("数字", "字母");

        ObservableList<String> listInteger = FXCollections.observableArrayList();
        listInteger.addAll("1", "2", "3", "4", "5", "6", "7");

        ObservableList<String> listStr3 = FXCollections.observableArrayList();
        listStr3.addAll("a", "b", "c", "d");


        ChoiceBox<String> choiceBox1 = new ChoiceBox<>();
        choiceBox1.setItems(listStr);

        ChoiceBox<String> choiceBox2 = new ChoiceBox<>();

        Button button = new Button("排序");

        choiceBox1.setPrefWidth(100d);
        choiceBox2.setPrefWidth(100d);
        AnchorPane.setTopAnchor(choiceBox1, 10d);
        AnchorPane.setTopAnchor(choiceBox2, 10d);
        AnchorPane.setTopAnchor(button, 10d);
        AnchorPane.setLeftAnchor(choiceBox1, 10d);
        AnchorPane.setLeftAnchor(choiceBox2, 130d);
        AnchorPane.setLeftAnchor(button, 250d);
        anchorPane.getChildren().addAll(choiceBox1, choiceBox2, button);

        primaryStage.setScene(scene);
        primaryStage.setTitle("choicebox");
        primaryStage.setWidth(625d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        choiceBox1.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue.equals("数字")) {
                choiceBox2.setItems(listInteger);
                choiceBox2.show();
            } else if (newValue.equals("字母")) {
                choiceBox2.setItems(listStr3);
                choiceBox2.show();
            }
        });

        button.setOnAction(event -> listInteger.sort((o1, o2) -> {
            int l1 = Integer.valueOf(o1);
            int l2 = Integer.valueOf(o2);
            return l2 - l1;
        }));
    }
}
